package com.facebook.drawee.view;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.StateListDrawable;
import android.util.AttributeSet;

import com.facebook.common.internal.Preconditions;
import com.facebook.drawee.drawable.AutoRotateDrawable;
import com.facebook.drawee.drawable.ScalingUtils;
import com.facebook.drawee.generic.GenericDraweeHierarchy;
import com.facebook.drawee.generic.GenericDraweeHierarchyBuilder;
import com.facebook.drawee.generic.RoundingParams;
import com.facebook.drawee.generic.RoundingParams.RoundingMethod;

import java.util.Arrays;

import javax.annotation.Nullable;

public class GenericDraweeView extends DraweeView {
    public GenericDraweeView(Context context) {
        super(context);
        inflateHierarchy(context, null);
    }

    public GenericDraweeView(Context context, AttributeSet attrs) {
        super(context, attrs);
        inflateHierarchy(context, attrs);
    }

    public GenericDraweeView(Context context, AttributeSet attrs, int defaultStyle) {
        super(context, attrs, defaultStyle);
        inflateHierarchy(context, attrs);
    }

    @TargetApi(value = 21)
    public GenericDraweeView(Context context, AttributeSet attrs, int defaultStyle, int arg4) {
        super(context, attrs, defaultStyle, arg4);
        inflateHierarchy(context, attrs);
    }

    public GenericDraweeView(Context context, GenericDraweeHierarchy genericDraweeHierarchy) {
        super(context);
        setHierarchy(genericDraweeHierarchy);
    }

    private void inflateHierarchy(Context context, @Nullable AttributeSet attributeSet) {
        Resources resources = context.getResources();
        int placeholderImage = 0;
        ScalingUtils.ScaleType placeholderImageScaleType = GenericDraweeHierarchyBuilder.DEFAULT_SCALE_TYPE;
        int retryImage = 0;
        ScalingUtils.ScaleType retryImageScaleType = GenericDraweeHierarchyBuilder.DEFAULT_SCALE_TYPE;
        int failureImage = 0;
        ScalingUtils.ScaleType failureImageScaleType = GenericDraweeHierarchyBuilder.DEFAULT_SCALE_TYPE;
        int progressBarImage = 0;
        ScalingUtils.ScaleType progressBarImageScaleType = GenericDraweeHierarchyBuilder.DEFAULT_SCALE_TYPE;
        ScalingUtils.ScaleType actualImageScaleType = GenericDraweeHierarchyBuilder.DEFAULT_ACTUAL_IMAGE_SCALE_TYPE;
        int backgroundImage = 0;
        int overlayImage = 0;
        int pressedStateOverlayImage = 0;
        boolean roundAsCircle = false;
        int roundedCornerRadius = 0;
        boolean roundTopLeft = true;
        boolean roundTopRight = true;
        boolean roundBottomRight = true;
        boolean roundBottomLeft = true;
        int overlayColor = 0;
        int borderWidth = 0;
        int borderColor = 0;
        int padding = 0;
        int progressBarAutoRotateInterval = 0;
        int fadeDuration = 300;
        if (attributeSet != null) {
            TypedArray gdhAttrs = context.obtainStyledAttributes(attributeSet, com.facebook.drawee.R.GenericDraweeHierarchy);
            try {
                int count = gdhAttrs.getIndexCount();
                for (int i = 0; i < count; ++i) {
                    int index = gdhAttrs.getIndex(i);
                    if (index == com.facebook.drawee.R.GenericDraweeHierarchy_actualImageScaleType) {
                        actualImageScaleType = GenericDraweeView.getScaleTypeFromXml(gdhAttrs, com.facebook.drawee.R.GenericDraweeHierarchy_actualImageScaleType, actualImageScaleType);
                    } else if (index == com.facebook.drawee.R.GenericDraweeHierarchy_placeholderImage) {
                        placeholderImage = gdhAttrs.getResourceId(com.facebook.drawee.R.GenericDraweeHierarchy_placeholderImage, placeholderImage);
                    } else if (index == com.facebook.drawee.R.GenericDraweeHierarchy_pressedStateOverlayImage) {
                        pressedStateOverlayImage = gdhAttrs.getResourceId(com.facebook.drawee.R.GenericDraweeHierarchy_pressedStateOverlayImage, pressedStateOverlayImage);
                    } else if (index == com.facebook.drawee.R.GenericDraweeHierarchy_progressBarImage) {
                        progressBarImage = gdhAttrs.getResourceId(com.facebook.drawee.R.GenericDraweeHierarchy_progressBarImage, progressBarImage);
                    } else if (index == com.facebook.drawee.R.GenericDraweeHierarchy_fadeDuration) {
                        fadeDuration = gdhAttrs.getInt(com.facebook.drawee.R.GenericDraweeHierarchy_fadeDuration, 300);
                    } else if (index == com.facebook.drawee.R.GenericDraweeHierarchy_viewAspectRatio) {
                        setAspectRatio(gdhAttrs.getFloat(com.facebook.drawee.R.GenericDraweeHierarchy_viewAspectRatio, getAspectRatio()));
                    } else if (index == com.facebook.drawee.R.GenericDraweeHierarchy_placeholderImageScaleType) {
                        placeholderImageScaleType = GenericDraweeView.getScaleTypeFromXml(gdhAttrs, com.facebook.drawee.R.GenericDraweeHierarchy_placeholderImageScaleType, placeholderImageScaleType);
                    } else if (index == com.facebook.drawee.R.GenericDraweeHierarchy_retryImage) {
                        retryImage = gdhAttrs.getResourceId(com.facebook.drawee.R.GenericDraweeHierarchy_retryImage, retryImage);
                    } else if (index == com.facebook.drawee.R.GenericDraweeHierarchy_retryImageScaleType) {
                        retryImageScaleType = GenericDraweeView.getScaleTypeFromXml(gdhAttrs, com.facebook.drawee.R.GenericDraweeHierarchy_retryImageScaleType, retryImageScaleType);
                    } else if (index == com.facebook.drawee.R.GenericDraweeHierarchy_failureImage) {
                        failureImage = gdhAttrs.getResourceId(com.facebook.drawee.R.GenericDraweeHierarchy_failureImage, failureImage);
                    } else if (index == com.facebook.drawee.R.GenericDraweeHierarchy_failureImageScaleType) {
                        failureImageScaleType = GenericDraweeView.getScaleTypeFromXml(gdhAttrs, com.facebook.drawee.R.GenericDraweeHierarchy_failureImageScaleType, failureImageScaleType);
                    } else if (index == com.facebook.drawee.R.GenericDraweeHierarchy_progressBarImageScaleType) {
                        progressBarImageScaleType = GenericDraweeView.getScaleTypeFromXml(gdhAttrs, com.facebook.drawee.R.GenericDraweeHierarchy_progressBarImageScaleType, progressBarImageScaleType);
                    } else if (index == com.facebook.drawee.R.GenericDraweeHierarchy_progressBarAutoRotateInterval) {
                        progressBarAutoRotateInterval = gdhAttrs.getInteger(com.facebook.drawee.R.GenericDraweeHierarchy_progressBarAutoRotateInterval, 0);
                    } else if (index == com.facebook.drawee.R.GenericDraweeHierarchy_backgroundImage) {
                        backgroundImage = gdhAttrs.getResourceId(com.facebook.drawee.R.GenericDraweeHierarchy_backgroundImage, backgroundImage);
                    } else if (index == com.facebook.drawee.R.GenericDraweeHierarchy_overlayImage) {
                        overlayImage = gdhAttrs.getResourceId(com.facebook.drawee.R.GenericDraweeHierarchy_overlayImage, overlayImage);
                    } else if (index == com.facebook.drawee.R.GenericDraweeHierarchy_roundAsCircle) {
                        roundAsCircle = gdhAttrs.getBoolean(com.facebook.drawee.R.GenericDraweeHierarchy_roundAsCircle, roundAsCircle);
                    } else if (index == com.facebook.drawee.R.GenericDraweeHierarchy_roundedCornerRadius) {
                        roundedCornerRadius = gdhAttrs.getDimensionPixelSize(com.facebook.drawee.R.GenericDraweeHierarchy_roundedCornerRadius, roundedCornerRadius);
                    } else if (index == com.facebook.drawee.R.GenericDraweeHierarchy_roundTopLeft) {
                        roundTopLeft = gdhAttrs.getBoolean(com.facebook.drawee.R.GenericDraweeHierarchy_roundTopLeft, roundTopLeft);
                    } else if (index == com.facebook.drawee.R.GenericDraweeHierarchy_roundTopRight) {
                        roundTopRight = gdhAttrs.getBoolean(com.facebook.drawee.R.GenericDraweeHierarchy_roundTopRight, roundTopRight);
                    } else if (index == com.facebook.drawee.R.GenericDraweeHierarchy_roundBottomRight) {
                        roundBottomRight = gdhAttrs.getBoolean(com.facebook.drawee.R.GenericDraweeHierarchy_roundBottomRight, roundBottomRight);
                    } else if (index == com.facebook.drawee.R.GenericDraweeHierarchy_roundBottomLeft) {
                        roundBottomLeft = gdhAttrs.getBoolean(com.facebook.drawee.R.GenericDraweeHierarchy_roundBottomLeft, roundBottomLeft);
                    } else if (index == com.facebook.drawee.R.GenericDraweeHierarchy_roundWithOverlayColor) {
                        overlayColor = gdhAttrs.getColor(com.facebook.drawee.R.GenericDraweeHierarchy_roundWithOverlayColor, overlayColor);
                    } else if (index == com.facebook.drawee.R.GenericDraweeHierarchy_roundingBorderWidth) {
                        borderWidth = gdhAttrs.getDimensionPixelSize(com.facebook.drawee.R.GenericDraweeHierarchy_roundingBorderWidth, borderWidth);
                    } else if (index == com.facebook.drawee.R.GenericDraweeHierarchy_roundingBorderColor) {
                        borderColor = gdhAttrs.getColor(com.facebook.drawee.R.GenericDraweeHierarchy_roundingBorderColor, borderColor);
                    } else if (index == com.facebook.drawee.R.GenericDraweeHierarchy_roundingBorderPadding) {
                        padding = gdhAttrs.getDimensionPixelSize(com.facebook.drawee.R.GenericDraweeHierarchy_roundingBorderPadding, padding);
                    }
                }
            } catch (Throwable throwable) {

            } finally {
                gdhAttrs.recycle();
            }
        }
        GenericDraweeHierarchyBuilder builder = new GenericDraweeHierarchyBuilder(resources);
        builder.DEFAULT_FADE_DURATION = fadeDuration;
        if (placeholderImage > 0) {
            builder.setPlaceholderImage(resources.getDrawable(placeholderImage), placeholderImageScaleType);
        }

        if (retryImage > 0) {
            builder.mRetryImage = resources.getDrawable(retryImage);
            builder.mRetryImageScaleType = retryImageScaleType;
        }

        if (failureImage > 0) {
            builder.mFailureImage = resources.getDrawable(failureImage);
            builder.mFailureImageScaleType = failureImageScaleType;
        }

        Drawable v3_3;
        if (progressBarImage > 0) {
            Drawable progressBarImageDrawable = resources.getDrawable(progressBarImage);
            if (progressBarAutoRotateInterval > 0) {
                v3_3 = new AutoRotateDrawable(progressBarImageDrawable, progressBarAutoRotateInterval);
            } else {
                v3_3 = progressBarImageDrawable;
            }

            builder.mProgressBarImage = v3_3;
            builder.mProgressBarImageScaleType = progressBarImageScaleType;
        }

        if (backgroundImage > 0) {
            builder.mBackgroundImageList = Arrays.asList(new Drawable[]{resources.getDrawable(backgroundImage)});
        }

        if (overlayImage > 0) {
            builder.mOverlays = Arrays.asList(new Drawable[]{resources.getDrawable(overlayImage)});
        }

        if (pressedStateOverlayImage > 0) {
            v3_3 = getResources().getDrawable(pressedStateOverlayImage);
            StateListDrawable stateListDrawable = new StateListDrawable();
            stateListDrawable.addState(new int[]{16842919}, v3_3);
            builder.mPressedStateOverlay = stateListDrawable;
        }

        builder.mActualImageScaleType = actualImageScaleType;
        if (roundAsCircle || roundedCornerRadius > 0) {
            RoundingParams roundingParams = new RoundingParams();
            roundingParams.mRoundAsCircle = roundAsCircle;
            if (roundedCornerRadius > 0) {
                if (roundingParams.mCornersRadii == null) {
                    roundingParams.mCornersRadii = new float[8];
                }
                roundingParams.mCornersRadii[1] = roundTopLeft ? (float) roundedCornerRadius : 0f;
                roundingParams.mCornersRadii[0] = roundTopLeft ? (float) roundedCornerRadius : 0f;
                roundingParams.mCornersRadii[3] = roundTopRight ? (float) roundedCornerRadius : 0f;
                roundingParams.mCornersRadii[2] = roundTopRight ? (float) roundedCornerRadius : 0f;
                roundingParams.mCornersRadii[5] = roundBottomRight ? (float) roundedCornerRadius : 0f;
                roundingParams.mCornersRadii[4] = roundBottomRight ? (float) roundedCornerRadius : 0f;
                roundingParams.mCornersRadii[7] = roundBottomLeft ? (float) roundedCornerRadius : 0f;
                roundingParams.mCornersRadii[6] = roundBottomLeft ? (float) roundedCornerRadius : 0f;
            }

            if (overlayColor != 0) {
                roundingParams.mOverlayColor = overlayColor;
                roundingParams.mRoundingMethod = RoundingMethod.OVERLAY_COLOR;
            }

            if (borderColor != 0 && borderWidth > 0) {
                roundingParams.setBorder(borderColor, (float) borderWidth);
            }

            if (padding != 0) {
                float padding2 = ((float) padding);
                Preconditions.checkArgument(padding2 >= 0f, "the padding cannot be < 0");
                roundingParams.mPadding = padding2;
            }
            builder.mRoundingParams = roundingParams;
        }
        setHierarchy(builder.build());
    }

    private static ScalingUtils.ScaleType getScaleTypeFromXml(TypedArray typedArray, int gdhAttrs, ScalingUtils.ScaleType scaleType) {
        int attr = typedArray.getInt(gdhAttrs, -1);
        if (attr >= 0) {
            scaleType = ScalingUtils.ScaleType.values()[attr];
        }
        return scaleType;
    }
}

